<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author ThinkGem
-->
<template>
  <BasicModal
    v-bind="$attrs"
    :title="t('设置首批')"
    :showOkBtn="true"
    :cancelText="t('common.closeText')"
    @register="registerModal"
    @ok="handleSubmit"
    :minHeight="200"
    width="20%"
  >
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'SetBatchForm',
    inheritAttrs: false,
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form';
  import { shopSalesPackageTreeData } from '/@/api/shop/sales/shopSalesPackage';
  import { saveBatchData } from '/@/api/bas/cus/basCus';
  import { useMessage } from '/@/hooks/web/useMessage';

  const { t } = useI18n('bpm.button');
  const { showMessage } = useMessage();

  const [registerModal, { closeModal, setModalProps }] = useModalInner(async (data: any) => {
    setModalProps({ loading: true });
    // 合并一次性设置，避免多次 setFieldsValue 覆盖已有字段
    const currentValues = getFieldsValue() || {};
    const nextValues: Record<string, any> = { ...currentValues };
    if (data?.record?.code) {
      nextValues.cusCode = data.record.code;
    }
    nextValues.batchType = data?.record?.cusExp?.batchType || '';
    nextValues.batchPages = data?.record?.cusExp?.batchPages || '';
    await setFieldsValue(nextValues);
    setModalProps({ loading: false });
  });

  const [registerForm, { setFieldsValue, validate, getFieldsValue }] = useForm({
    labelWidth: 120,
    schemas: [
      {
        label: t('客户编码'),
        field: 'cusCode',
        component: 'Input',
        required: true,
      },
      {
        label: '首批类型',
        field: 'batchType',
        component: 'Select',
        componentProps: {
          dictType: 'btdm_package_type',
          allowClear: true,
        },
        // defaultValue: '1',
        required: true,
        itemProps: { validateTrigger: 'blur' },
      },
      {
        label: t('首批包'),
        field: 'batchPages',
        fieldLabel: 'batchPages',
        component: 'TreeSelect',
        componentProps: {
          api: shopSalesPackageTreeData,
          allowClear: true,
          // immediate: true,
          // onChange: async(id)=>{
          //   console.log(soAdvanceOrderChildList.value,'soAdvanceOrderChildList.value==');
          //   await testDataChildTable.setTableData([])
          //   if(id){
          //     openxqDrawer(true, {id,showFooter:true});
          //   }
          // }
        },
        required: true,
        itemProps: { validateTrigger: 'blur' },
      },
    ],
    baseColProps: { lg: 24, md: 24 },
  });

  const handleSubmit = async () => {
    try {
      setModalProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: false,
      };
      const validateData = await validate();
      // console.log(validateData, 'validate==');
      // const obj = {
      //   cusExp: validateData,
      // };
      const res = await saveBatchData(params, validateData);
      showMessage(res.message);
      setModalProps({ confirmLoading: false });
      setTimeout(closeModal);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage('您填写的信息有误，请根据提示修正。');
      }
      console.log('error', error);
    } finally {
      setModalProps({ confirmLoading: false });
    }
  };
</script>
